package com.artPreter.controller.pc;

import com.artPreter.pojo.*;
import com.artPreter.service.*;
import com.baomidou.mybatisplus.core.metadata.IPage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import static com.artPreter.utils.PageUtils.getPageList;

@Controller
public class ArtworkController {

    @Autowired
    private IArtworkService artWorkService;
    @Autowired
    private IDepartContentSectionService departContentSectionService;
    @Autowired
    private IArtTypeService artTypeService;
    @Autowired
    private ISystemConfigService systemConfigService;
    @Autowired
    private IArtworkOtherPicService artworkOtherPicService;


    /**
     * 艺术品详情页
     *
     * @param id
     * @param model
     * @return
     */
    @RequestMapping("/artwork/detail")
    public String index(Integer id, Model model) {
        System.out.println("/artwork/detail11111111");
        // 系统设置
        HashMap systemConfig = systemConfigService.getSystemConfig();
        model.addAttribute("systemConfig",systemConfig);

        // 头部菜单
        List<DepartContentSection> menuList = departContentSectionService.getMenuList();
        model.addAttribute("menus", menuList);
        Artwork artwork = artWorkService.getArtWorkById(id);


        if (artwork != null) {
            List<ArtType> artTypes = artTypeService.getList();
            for (int i = 0; i < artTypes.size(); i++) {
                if (artTypes.get(i).getTypeId().equals(artwork.getArtwoktType())) {
                    artwork.setArtTypeName(artTypes.get(i).getName());
                }
            }
            model.addAttribute("artwork", artwork);
            return "pc/artwork/artwork_page_1";
        } else {
            return "redirect:/";
        }
    }

    /**
     * 艺术品列表
     *
     * @param
     * @param model
     * @return
     */
    @RequestMapping("/artwork/list")
    public String artworkList(Integer type, Integer currentPage, String name, Model model) {

        if (currentPage == null) {
            currentPage = 1;
        }

        // 系统设置
        HashMap systemConfig = systemConfigService.getSystemConfig();
        model.addAttribute("systemConfig",systemConfig);


        // 头部菜单
        List<DepartContentSection> menuList = departContentSectionService.getMenuList();

        List<ArtType> artTypes = new ArrayList<>();
        artTypes = artTypeService.getList();
        model.addAttribute("artTypes", artTypes);

        model.addAttribute("menus", menuList);

        Artwork artwork = new Artwork();
        // 艺术类型查询
        if (type != null) {
            for (int i = 0; i < artTypes.size(); i++) {
                if(artTypes.get(i).getId().equals(type)){
                    artwork.setArtwoktType(artTypes.get(i).getTypeId());
                }
            }
        }
        if (name == null || name.equals("") || name.equals("null")) {
            name = "";
        } else {
            artwork.setName(name);
        }

        Integer artWorkSize = 16;
        IPage<Artwork> artworkIPage = artWorkService.getPage(currentPage, artWorkSize, artwork);
        // 当前页
        model.addAttribute("currentPage", artworkIPage.getCurrent());
        System.out.println(artworkIPage.getPages() + "总页数");
        // 总页数
        model.addAttribute("totalPages", artworkIPage.getPages());
        // 分页页码
        model.addAttribute("pages", getPageList((int) artworkIPage.getPages(), currentPage, 5));
        // 分页数据
        model.addAttribute("artworks", artworkIPage.getRecords());
        model.addAttribute("keyword", name);
        model.addAttribute("type", type);
        return "pc/company/artwork_list";


    }

    /**
     *
     * @param id 艺术品id
     * @param
     * @param model
     * @return
     */
    @GetMapping("/artwork/detail")
    public String onlineArtworkDetail(Integer id, Model model) {
        System.out.println("/artwork/detail22222");
        // 系统设置
        HashMap systemConfig = systemConfigService.getSystemConfig();
        model.addAttribute("systemConfig",systemConfig);
        // 头部菜单
        List<DepartContentSection> menuList = departContentSectionService.getMenuList();
        model.addAttribute("menus",menuList);
        for (int i = 0; i < menuList.size(); i++) {
            System.out.println(menuList.get(i).getContent());
            if(menuList.get(i).getContent().equals("/artwork/list")){
                menuList.get(i).setIsActice(1);
                model.addAttribute("backName",menuList.get(i).getName());
                model.addAttribute("backContent",menuList.get(i).getContent());
            }
        }




        // 首页显示艺术类型
        List<ArtType> artTypes = artTypeService.getList();
        model.addAttribute("artTypes",artTypes);

        Artwork artwork = artWorkService.getArtWorkById(id);



        if(artwork!=null&&artwork.getId()!=null){
            if(artwork.getArtwoktType()!=null) {
                for (int i = 0; i < artTypes.size(); i++) {
                    if(artTypes.get(i).getTypeId().equals(artwork.getArtwoktType())){
                        artwork.setArtTypeName(artTypes.get(i).getName());
                    }
                }
            }
            List<ArtworkOtherPic> artworkOtherPics = artworkOtherPicService.getArtworkAllPicList(artwork);
            model.addAttribute("artworkOtherPics",artworkOtherPics);
            model.addAttribute("artwork",artwork);
            return "pc/company/artwork_detail";
        } else {
            return "redirect:/";
        }
    }
}
